﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentManagerSystem
{
    public class InputManager
    {
        private static readonly InputManager _instance = new InputManager();

        
        private InputManager()
        {

        }
        
        public static InputManager Instance
        {
            get { return _instance; }
        }

        StudentsSystem StudentsSystem;
        
        public InputManager(StudentsSystem ss)
        {
            StudentsSystem = ss;
            Console.Title = "学生成绩管理系统";
            Console.SetWindowSize(88, 50);
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.Black;
            
        }
        //登录界面
        public void LoginPanel()
        {
            //清屏
            Console.Clear();
            PrintTitle();
            PrintMenu(1, "输入账号密码");
            PrintMenu(2, "退出系统");
            PrintMenuEnd();

            do
            {
                int op;
                Write("请输入需要操作的编号:");
                while (!int.TryParse(Console.ReadLine(), out op) || op < 1 || op > 2)
                {
                    Write("\n输入错误，请重新输入:");
                }
                Console.WriteLine(op);
                switch (op)
                {
                    case 1:
                        InputAccountPassword();
                        return;
                    case 2:
                        return;
                }
            } while (true);

            /*
            
            bool flag= false;
            do
            {
                int op;
                if (flag)
                    Write("输入错误，请重新输入需要操作的编号:");
                else
                    Write("请输入需要操作的编号:");
                flag = false;
                while (!int.TryParse(Console.ReadLine(), out op) || op <1 || op > 2)
                {
                    Write("\n输入错误，请重新输入:");
                }
                Console.WriteLine(op);
                switch (op)
                {
                    case 1:
                        InputAccountPassword();
                        return;
                    case 2:
                        return;
                    default:
                        flag = true;
                        break;
                }
            } while (flag);
            */
        }

        //主界面
        public void MainPanel()
        {
            bool PermissionFlag = StudentsSystem.Permission == E_PermissionType.Administrator ||
                                    StudentsSystem.Permission == E_PermissionType.Root ||
                                    StudentsSystem.Permission == E_PermissionType.Teacher;

            //清屏
            Console.Clear();
            PrintTitle();
            PrintMenu(1, "信息查询");
            PrintMenu(2, "成绩分析");
            PrintMenu(3, "成绩排名");
            if (PermissionFlag==true)
            {
                PrintMenu(4, "信息录入");
                PrintMenu(5, "信息修改");
            }
            PrintMenu(0, "返回上一层");
            PrintMenuEnd();

            bool flag = false;
            do
            {
                int op;
                if (flag)
                    Write("输入错误，请重新输入需要操作的编号:");
                else
                    Write("请输入需要操作的编号:");
                flag = false;
                while (!int.TryParse(Console.ReadLine(), out op))
                {
                    Write("\n输入错误，请重新输入需要操作的编号:");
                }
                Console.WriteLine(op);
                switch (op)
                {
                    case 1:
                        CheckStudentInfo();
                        return;
                    case 2:
                        MainPanel();
                        return;
                    case 3:
                        CheckScorePanel();
                        return;
      
                    case 4:
                        if (PermissionFlag == true)
                        {
                            InputInfoPanel();
                            return;
                        }else
                        {
                            flag = true;
                            break;
                        }
                    case 5:
                        if (PermissionFlag == true)
                        {
                            ModifyInfoPanel();
                            return;
                        }
                        else
                        {
                            flag = true;
                            break;
                        }
                    case 0:
                        LoginPanel();
                        return;
                    default:
                        flag = true;
                        break;
                }
            } while (flag);

        }

        public void CheckScorePanel(int op)
        {
            bool flag = false;
            do
            {
                flag = false;
                E_GradeType grade = E_GradeType.None;
                E_CourseType course = E_CourseType.None;
                int classNum = 0;
                int temp;
                string ts;
                //录入年级
                Write("\n请输入年级(1/2/3/4):");
                while (!int.TryParse(Console.ReadLine(), out temp) || (temp < 1 || temp > 4))
                {
                    Write("\n输入错误，请重新输入年级(1/2/3/4):", ConsoleColor.Red);
                }
                grade = (E_GradeType)temp;
                //录入班级
                if (op == 1 || op == 2)
                {
                    Write("\n请输入班级(1~24):");
                    while (!int.TryParse(Console.ReadLine(), out temp) || (temp < 1 || temp > 24))
                    {
                        Write("\n输入错误，请重新输入班级(1~24):", ConsoleColor.Red);
                    }
                    classNum = temp;
                }

                //录入科目
                if (op == 2 || op == 4)
                {
                    Write("\n请选择科目\n\t1:English\n\t2:Chinese\n\t3:Geography\n\t4:History\n\t5:Biology\n\t6:Chemistry \n科目代号:");
                    while (!int.TryParse(Console.ReadLine(), out temp) || (temp < 1 || temp > 6))
                    {
                        Write("\n输入错误，请选择科目\n\t1:English\n\t2:Chinese\n\t3:Geography\n\t4:History\n\t5:Biology\n\t6:Chemistry \n科目代号:", ConsoleColor.Red);
                    }
                    course = (E_CourseType)temp;
                }

                List<Student> list = StudentsSystem.GetStudents(grade, classNum, course);
                PrintLineStart();
                for (int i = 0; i < list.Count; i++)
                {
                    PrintScore(list[i], course);
                }
                PrintLineStart();

                Write("\n信息查询完成;是否继续查看？（Y/N）");

                ts = Console.ReadLine();
                if (ts == "Y" || ts == "y")
                {
                    flag = true;
                }
                else
                {
                    //返回上一层
                    CheckScorePanel();
                    return;
                }

            } while (true);
            
        }

        /// <summary>
        /// 打印学生成绩
        /// </summary>
        /// <param name="s"></param>
        /// <param name="course">course 为 None时输出全部科目，否则输出指定科目</param>
        private void PrintScore(Student s, E_CourseType course = E_CourseType.None)
        {
            if (course == E_CourseType.None)
            {
                Write(string.Format("学号:{0}\t名字:{1}", s.ID, s.Name));
                for (E_CourseType i = E_CourseType.None; i < E_CourseType.All; i++)
                {
                    Write(string.Format("\t{0}:{1}",(i+1), s.Score.GetScore(i+1)));
                }
            }else
            {
                Write(string.Format("学号:{0}\t名字:{1} {2}:{3}", s.ID, s.Name, course, s.Score.GetScore(course)));
            }
            PrintLine();
        }

        public void CheckScorePanel()
        {
            //清屏
            Console.Clear();
            PrintTitle();
            PrintMenu(1, "查询指定班级总分排名");
            PrintMenu(2, "查询指定班级科目排名");
            PrintMenu(3, "查询指定年级总分排名");
            PrintMenu(4, "查询指定年级科目排名");
            PrintMenu(0, "返回上一层");
            PrintMenuEnd();

            bool flag = false;
            do
            {
                int op;
                if (flag)
                    Write("输入错误，请重新输入需要操作的编号:");
                else
                    Write("请输入需要操作的编号:");
                flag = false;
                while (!int.TryParse(Console.ReadLine(), out op))
                {
                    Write("\n输入错误，请重新输入:");
                }
                Console.WriteLine(op);

                if (op == 0)
                    MainPanel();
                else if (op > 0 && op < 5)
                    CheckScorePanel(op);
                else
                    flag = true;
            } while (flag);
        }

        //信息查看子界面
        public void CheckStudentInfo()
        {
            //清屏
            Console.Clear();
            PrintTitle();
            PrintMenu(1, "按照学号查看指定学生");
            PrintMenu(2, "按照名字查看指定学生");
            PrintMenu(0, "返回上一层");
            PrintMenuEnd();

            bool flag = false;
            do
            {
                int op;
                if (flag)
                    Write("输入错误，请重新输入需要操作的编号:");
                else
                    Write("请输入需要操作的编号:");
                flag = false;
                while (!int.TryParse(Console.ReadLine(), out op))
                {
                    Write("\n输入错误，请重新输入:");
                }
                Console.WriteLine(op);
                switch (op)
                {
                    case 1:
                        CheckStudentInfo(1);
                        return;
                    case 2:
                        CheckStudentInfo(2);
                        return;
                    case 0:
                        MainPanel();
                        return;
                    default:
                        flag = true;
                        break;
                }
            } while (flag);
        }

        //信息查看
        public void CheckStudentInfo(int operatorFlag)
        {
            //清屏
            Console.Clear();
            PrintSubTitle("信息查询");

            bool flag = false;
            do
            {
                flag = false;
                //学号
                int op;
                string temp;
                Student checkStudent;
                //按照学号修改
                if (operatorFlag == 1)
                {
                    do
                    {
                        Write("请输入6位数字学号:");
                        while (!int.TryParse(Console.ReadLine(), out op) || (op < 100000 || op > 999999))
                        {
                            Write("\n输入错误，请重新输入6位数字学号:", ConsoleColor.Red);
                        }
                        checkStudent = StudentsSystem.GetStudent(op);
                        if (checkStudent == null)
                        {
                            Write("\n输入的学号：" + op.ToString() + "，没有找到该学生信息！！！", ConsoleColor.Red);
                        }
                    } while (checkStudent == null);
                }
                else //按照名字修改
                {
                    do
                    {
                        Write("请输入名字:");
                        temp = Console.ReadLine();
                        checkStudent = StudentsSystem.GetStudent(temp);
                        if (checkStudent == null)
                        {
                            Write("\n输入的名字：" + temp + "，没有找到该学生信息！！！", ConsoleColor.Red);
                        }
                    } while (checkStudent == null);
                }

                Write("信息查询完成...\n", ConsoleColor.Blue);

                Write(checkStudent.ToString(), ConsoleColor.Green);
                
                Write("\n信息查询完成;是否继续查看？（Y/N）");

                temp = Console.ReadLine();
                if (temp == "Y" || temp == "y")
                {
                    flag = true;
                }
                else
                {
                    //返回上一层
                    CheckStudentInfo();
                    return;
                }

            } while (flag);
        }

        //输入一个学生信息
        private Student InputStudent()
        {
            Student s = new Student();
            //学号
            int op;
            Write("请输入6位数字学号:");
            while (!int.TryParse(Console.ReadLine(), out op) || (op < 100000 || op > 999999))
            {
                Write("\n输入错误，请重新输入6位数字学号:");
            }
            s.ID = op;

            //名字
            string temp;
            Write("请输入名字:");
            temp = Console.ReadLine();
            s.Name = temp;

            //性别
            Write("请输入性别(1.男生  2.女生)的数字代码:");
            while (!int.TryParse(Console.ReadLine(), out op) || (op < 0 || op > 2))
            {
                Write("\n输入错误，请重新输入性别(1.男生  2.女生)的数字代码:", ConsoleColor.Red);
            }
            s.Sex = (E_SexType)op;

            //年级
            Write("请输入年级(1/2/3/4)的数字代码:");
            while (!int.TryParse(Console.ReadLine(), out op) || (op < 0 || op > 4))
            {
                Write("\n输入错误，请重新输入年级(1/2/3/4)的数字代码:", ConsoleColor.Red);
            }
            s.Grade = (E_GradeType)op;

            //班级
            Write("请输入班级(1~24)的数字:");
            while (!int.TryParse(Console.ReadLine(), out op) || (op < 0 || op > 24))
            {
                Write("\n输入错误，请重新输入班级(1~24)的数字:", ConsoleColor.Red);
            }
            s.Class = op;

            return s;
        }
        //学生信息录入
        public void InputInfoPanel()
        {
            //清屏
            Console.Clear();
            PrintSubTitle("信息录入");

            bool flag = false;
            do
            {
                flag = false;
                string temp;
                Student s = InputStudent();

                Write("信息输入完成，录入中...\n", ConsoleColor.Blue);
                Write(s.ToString(), ConsoleColor.DarkBlue);

                StudentsSystem.AddStudent(s);

                Write("\n信息录入完成;是否继续录入？（Y/N）");

                temp = Console.ReadLine();
                if (temp == "Y" || temp == "y")
                {
                    flag = true;
                }
                else
                {
                    //返回主页面
                    MainPanel();
                    return;
                }

            } while (flag);
        }

        //学生信息修改主界面
        public void ModifyInfoPanel()
        {
            //清屏
            Console.Clear();
            PrintTitle();
            PrintMenu(1, "按照学号修改指定学生");
            PrintMenu(2, "按照名字修改指定学生");
            PrintMenu(0, "返回上一层");
            PrintMenuEnd();

            bool flag = false;
            do
            {
                int op;
                if (flag)
                    Write("输入错误，请重新输入需要操作的编号:");
                else
                    Write("请输入需要操作的编号:");
                flag = false;
                while (!int.TryParse(Console.ReadLine(), out op))
                {
                    Write("\n输入错误，请重新输入:");
                }
                Console.WriteLine(op);
                switch (op)
                {
                    case 1:
                        ModifyInfoPanel(1);
                        return;
                    case 2:
                        ModifyInfoPanel(2);
                        return;
                    case 0:
                        MainPanel();
                        return;
                    default:
                        flag = true;
                        break;
                }
            } while (flag);

        }

        //学生信息录入子界面
        public void ModifyInfoPanel(int operatorFlag)
        {
            //清屏
            Console.Clear();
            PrintSubTitle("信息修改");

            bool flag = false;
            do
            {
                flag = false;
                //学号
                int op;
                string temp;
                Student modifyStudent;
                //按照学号修改
                if (operatorFlag == 1)
                {
                    do
                    {
                        Write("\n请输入6位数字学号:");
                        while (!int.TryParse(Console.ReadLine(), out op) || (op < 100000 || op > 999999))
                        {
                            Write("\n输入错误，请重新输入6位数字学号:", ConsoleColor.Red);
                        }
                        modifyStudent = StudentsSystem.GetStudent(op);
                        if (modifyStudent==null)
                        {
                            Write("\n输入的学号："+ op.ToString() + "，没有找到该学生信息！！！", ConsoleColor.Red);
                        }
                    } while (modifyStudent == null);
                }
                else //按照名字修改
                {
                    do
                    {
                        Write("\n请输入名字:");
                        temp = Console.ReadLine();
                        modifyStudent = StudentsSystem.GetStudent(temp);
                        if (modifyStudent == null)
                        {
                            Write("\n输入的名字：" + temp + "，没有找到该学生信息！！！", ConsoleColor.Red);
                        }
                    } while (modifyStudent == null);
                }

                Write("信息查询完成...\n", ConsoleColor.Blue);
                Write(modifyStudent.ToString(), ConsoleColor.DarkBlue);

                Write("新信息录入开始...\n",ConsoleColor.Blue);

                Student s = InputStudent();

                Write("信息录入完成...\n", ConsoleColor.Blue);

                Write(s.ToString(), ConsoleColor.DarkBlue);

                StudentsSystem.RemoveStudent(modifyStudent.ID);
                StudentsSystem.AddStudent(s);

                Write("\n信息修改完成;是否继续修改？（Y/N）");

                temp = Console.ReadLine();
                if (temp == "Y" || temp == "y")
                {
                    flag = true;
                }
                else
                {
                    //返回上一层
                    ModifyInfoPanel();
                    return;
                }

            } while (flag);
        }
        //输入账号密码
        private void InputAccountPassword()
        {
            bool flag = false;
            do
            {

                if (flag)
                    Write("账号或密码错误，请重新输入账号:");
                else
                    Write("请输入账号:");
                flag = false;
                string account = Console.ReadLine();

                Write("\n请输入密码:");
                string password = Console.ReadLine();

                if (StudentsSystem.CheckAccountPassword(account, password))
                {
                    MainPanel();
                    return;
                }
                else
                {
                    flag = true;
                }

            } while (flag);
        }

        //输出一行菜单
        private void PrintMenu(int index, string s)
        {
            PrintLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("            ***  {0}  ***    |    {1}", index, s);
            Console.ForegroundColor = ConsoleColor.Black;
            PrintLine();
        }

        private void Write(string s, ConsoleColor c = ConsoleColor.Black)
        {
            Console.ForegroundColor = c;
            Console.Write(s);
            Console.ForegroundColor = ConsoleColor.Black;
        }
        
        //输出一行*
        private void PrintLineStart()
        {
            Console.WriteLine("**************************************************************************************");
        }

        //输出一个空行
        private void PrintLine()
        {
            Console.WriteLine();
        }

        //输出学生系统菜单尾
        private void PrintMenuEnd()
        {
            PrintLineStart();
            PrintLineStart();
        }

        //输出学生系统标题头
        private void PrintTitle()
        {
            PrintLineStart();
            Console.WriteLine("*********************************  学生成绩管理系统  *********************************");
            PrintLineStart();
        }

        //输出学生系统标题头
        private void PrintSubTitle(string title)
        {
            PrintLineStart();
            Console.WriteLine("                          {0}  ", title);
            PrintLineStart();
        }
    }
}
